#include "mainwindow.h"

#include "gif.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMovie>

extern "C" {
#include "gifico.h"
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    auto m = new QMovie(QFileDialog::getOpenFileName(this,
                                                     "gif",
                                                     "E:/ARM/sources/giflib-5.2.2/pic",
                                                     "image(*.gif)"));
    ui->giflabel->setMovie(m);
    m->start();

    //    char fs[50] = R"(E:\ARM\sources\giflib-5.2.2\pic\fire.gif)";
    //    char nameTable[20] = "UNKNOWN.ico";

    //    Gif2Icon(fs, -1, -1, nameTable);
    //    Icon2Gif(R"(E:\ARM\sources\giflib-5.2.2\tests\gifgrid.ico)",
    //             nullptr,
    //             false,
    //             _fileno(fopen("demo.gif", "wb+")));

    //    auto p = QPixmap("");
    //    auto img = QImage("");

    Gif::GifWriter writer;
    Gif g;
    if (g.GifBegin(&writer, "demo2.gif", width(), height(), 0)) { //0.01 unit
        for (int i = 0; i < 100; ++i) {
            g.GifWriteFrame(&writer, grab().toImage().bits(), width(), height(), 0);
        }
    }

    g.GifEnd(&writer);
}
